import { Injectable } from '@angular/core';
import { APIBase } from 'app/modules/base/api-base';
import { HttpClient } from '@angular/common/http';
import { toTree } from 'app/modules/share/components/tree/tree.component';

@Injectable({
  providedIn: 'root',
})
export class AdminService extends APIBase<FormData, AdminModel> {
  constructor(private http: HttpClient) {
    super(http, '/api/admin/admin');
  }
  async tree() {
    const resp = await this.list();
    return toTree(resp.list, 'level_struct');
  }

  async get(id: number) {
    const admin_info = await super.get(id);
    admin_info.area_ids = (admin_info.area_ids || []).map(Number);
    return admin_info;
  }

  // 所有管理员，用于站内信
  all_admin() {
    return new APIBase(this.http, '/api/admin/all_admin');
  }

  // 管理菜单可见性
  menu_visibility() {
    interface resp {
      system_config: boolean;
    }
    return this.http.get<resp>('/api/admin/menu_visibility').toPromise();
  }
  // 权限节点列表
  auth_nodes() {
    return this.http.get<AutoNode[]>('/api/admin/auth');
  }
  reset_password(param: { id: number; login_password: string; new_password: string }) {
    return this.http.patch(this.baseURL + '/password', param).toPromise();
  }
}

export interface AdminModel {
  id: number;
  level_struct: string;
  parent_admin?: string;
  full_name: string;
  username: string;
  password?: string;
  remark: string;
  area_count: number;
  user_count: number;
  pc_count: number;
  software_count: number;
  last_login_time: string;
  area_ids?: number[];
  auth_ids?: number[];
  children?: AdminModel[];
}

export interface AutoNode {
  id: number;
  name?: string;
}

export enum AutoNodeConst {
  CreateAdminUser = 1,
  CreateZone = 2,
}
